package com.ubxtech.core.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import org.springframework.util.AntPathMatcher;

import java.util.Arrays;
import java.util.Collection;

/**
 * @author Victor.Xiao
 * @since 2023-10-18 14:10
 **/
public class AntPathMatcherUtil {

    private static final AntPathMatcher ANT_PATH_MATCHER = new AntPathMatcher();

    /**
     * 单个比较url
     * @author Victor.Xiao
     * @since 2023-10-18 15:01
     * @param path 要匹配的路径
     * @param pattern 要匹配的表达式
     * @return boolean
    */
    public static boolean match(String path,String pattern) {
        return ANT_PATH_MATCHER.match(pattern,path);
    }

    /**
     * 批量比较url
     * @author Victor.Xiao
     * @since 2023-10-18 15:01
     * @param path 要匹配的路径
     * @param patterns 要匹配的表达式
     * @return boolean
     */
    public static boolean match(String path, Collection<String> patterns) {
        if (CollectionUtil.isEmpty(patterns) || StrUtil.isBlank(path)) {
            return false;
        }
        return patterns.stream().anyMatch(pattern -> ANT_PATH_MATCHER.match(pattern, path));
    }

    public static void main(String[] args) {
        System.out.println(AntPathMatcherUtil.match("/test/sss/aa", "/test/**"));
        System.out.println(AntPathMatcherUtil.match("/test/sss/aa", Arrays.asList("/test/bb","/url/sss/**")));
    }
}
